
export const cFetch=<T>(url:string,opts?:CustomClientFetchOptions)=>{
    const config =useRuntimeConfig()
    return $fetch<ApiResponse<T> >(url,{
        baseURL:config.public.baseURL,
        onRequest:({options}) => {
            //从自定义配置中知道是否需要携带身份验证信息
            if(opts?.auth){
                let token:string=userStore().getToken()
                options.headers.set('Authorization', `Bearer ${token}`)
            }
        },
        onResponse:({response})=>{
            if(response.status >= 200 && response.status < 300) {
                if(response._data.code != 200) {
                    ElMessage.error(response._data.message)
                }
            }
        },
        onResponseError:({response})=>{
            if(response.status===401){
                ElMessage.warning('登录信息过期')
                navigateTo('/login')
                return
            }
            ElMessage.error( response._data.message)
        },
        ...opts?.defaultOpts
    })
}